package cn.aishua.ui.myKaka;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.apache.http.HttpResponse;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import cn.aishua.R;
import cn.aishua.network.HttpRequestUtil;
import cn.aishua.ui.myKaka.task.DataExchangeAsyncTask;
import cn.aishua.util.Constant;

public class AddBankCardActivity
    extends Activity
{

    private Spinner spBankType;
    private RadioGroup rgCardType;
    private EditText etCardNum;

    ArrayAdapter<String> adapter;

    //    private CheckBox cbSetExchange;

    // page header widgets
    private ImageView ivLeftBtn;
    private ImageView ivRightBtn;
    private TextView tvTitile;

    private void initPageHeader()
    {
        // left button
        ivLeftBtn = (ImageView) findViewById( R.id.page_header_iv_left_btn );
        ivLeftBtn.setImageResource( R.drawable.btn_back );
        ivLeftBtn.setOnClickListener( new View.OnClickListener()
        {

            @Override
            public void onClick( View v )
            {
                onBackPressed();
            }
        } );

        // title
        tvTitile = (TextView) findViewById( R.id.page_header_tv_title );
        tvTitile.setText( "添加银行卡" );

        // right button
        ivRightBtn = (ImageView) findViewById( R.id.page_header_iv_right_btn );
        ivRightBtn.setImageResource( R.drawable.btn_confirm );
        ivRightBtn.setOnClickListener( new View.OnClickListener()
        {

            @Override
            public void onClick( View v )
            {
                submit( v );
            }
        } );
    }

    @Override
    protected void onCreate( Bundle savedInstanceState )
    {
        Log.d( "AddBankCardActivity", "onCreate" );
        super.onCreate( savedInstanceState );
        setContentView( R.layout.my_kaka_add_bank_card );
        initPageHeader();
        initiate();
    }

    private void initiate()
    {
        spBankType = (Spinner) findViewById( R.id.my_kaka_add_bank_card_sp_bank_type );
        List<String> bankList = getBankList();
        adapter = new ArrayAdapter<String>( this, R.layout.my_spinner, bankList );
        adapter.setDropDownViewResource( R.layout.my_spinner_shake_dropdown );
        spBankType.setAdapter( adapter );

        etCardNum = (EditText) findViewById( R.id.my_kaka_add_bank_card_et_card_num );

        rgCardType = (RadioGroup) findViewById( R.id.my_kaka_add_bank_card_rg_card_type );
    }

    private List<String> getBankList()
    {
        List<String> bankList = new ArrayList<String>();
        Set<String> banks = Constant.BANK_ICON_MAP.keySet();
        for( String bank : banks )
        {
            bankList.add( bank );
        }
        return bankList;
    }

    public void back( View view )
    {
        onBackPressed();
    }

    public void submit( View view )
    {
        String bankType = spBankType.getSelectedItem().toString();
        String cardNum = etCardNum.getText().toString();

        if( cardNum.length() < 16 )
        {
            Toast.makeText( this, "请输入正确的银行卡号", Toast.LENGTH_SHORT ).show();
            return;
        }

        int checkedId = rgCardType.getCheckedRadioButtonId();
        String cardType = ""; // 0 -
        if( checkedId != -1 )
        {
            cardType = ( (RadioButton) findViewById( checkedId ) ).getText().equals( "储蓄卡" ) ? "0" : "1";
        }

        new DataExchangeAsyncTask( this, "添加银行卡" )
        {

            @Override
            protected HttpResponse sendRequest( String... params )
            {
                return HttpRequestUtil.getInstance().addBankCard( params );
            }

            @Override
            protected void handleSuccResponse( String strJson )
            {

                Toast.makeText( AddBankCardActivity.this, "银行卡已添加", Toast.LENGTH_SHORT ).show();
                finish();// to back to bank card list
            }
        }.execute( bankType, cardType, cardNum );
    }
    //    }
}
